import { IconDefinition, ThemeType } from "./types";
import { isDevMode } from '@angular/core';
import { generate as generateColor } from '@ant-design/colors';

export const Sandmon_ICON_ANGULAR_CONSOLE_PREFIX = '[@sandmon/icons-angular]:';

export function warn (message: string): void {
  if (isDevMode()) {
    console.warn(`${Sandmon_ICON_ANGULAR_CONSOLE_PREFIX} ${message}.`)
  }
}

export function error (message: string): void {
  console.error(`${Sandmon_ICON_ANGULAR_CONSOLE_PREFIX} ${message}.`);
}

export function isIconDefinition (target: string | IconDefinition): target is IconDefinition {
  return (
    typeof target === 'object' &&
    typeof target.name === 'string' &&
    (typeof target.theme === 'string' || typeof target.theme === undefined) &&
    typeof target.icon === 'string'
    )
}

export function getNameAndNamespace(name: string): [string, string] {
  const split = name.split(':')
  switch (split.length) {
    case 1:
      return [name, ''];
    case 2:
      // 自定义命名空间
      return [split[1], split[0]];
    default:
      throw new Error(`${Sandmon_ICON_ANGULAR_CONSOLE_PREFIX}The icon type ${name} is not valid!`)
  }
}

export function alreadyHasThemeSuffix(name: string): boolean {
  return name.endsWith('-fill') || name.endsWith('-outline') || name.endsWith('-twotone');
}

export function withThemeSuffix(name: string, theme: ThemeType): string {
  switch(theme) {
    case 'fill' || 'outline' || 'twotone':
      return `${name}-${theme}`;
    case undefined:
      return name;
    default:
      throw new Error(`${Sandmon_ICON_ANGULAR_CONSOLE_PREFIX}Theme "${theme}" is not a recognized theme!`)
  }
}

export function withSuffixAndColor(name: string, theme: ThemeType, pri: string, sec: string): string {
  return `${withThemeSuffix(name, theme)}-${pri}-${sec}`;
}

export function getIconDefinitionFromAbbr(str: string): IconDefinition {
  const arr = str.split('-');
  const theme = arr.splice(arr.length - 1, 1)[0]
  const name = arr.join('-');
  return {
    name,
    theme,
    icon: ''
  } as IconDefinition
}

export function getSecondaryColor(primaryColor: string): string {
  return generateColor(primaryColor)[0];
}

export function hasNamespace(type: string): boolean {
  return getNameAndNamespace(type)[1] !== '';
}

export function replaceFillColor(raw: string): string {
  return raw
    .replace(/['"]#333['"]/g, '"primaryColor"')
    .replace(/['"]#E6E6E6['"]/g, '"secondaryColor"')
    .replace(/['"]#D9D9D9['"]/g, '"secondaryColor"')
    .replace(/['"]#D8D8D8['"]/g, '"secondaryColor"');
}